//
//  oldMessageAlertView.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/14.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMessageAlertView.h"
#import "AppDelegate.h"

@interface oldMessageAlertView()
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)UIWindow *window;
@end

@implementation oldMessageAlertView

+(instancetype)shareInstance
{
    static oldMessageAlertView *stateView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        stateView = [[oldMessageAlertView alloc]init];
    });
    return stateView;
}

-(instancetype)init
{
    self = [super init];
    id <UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate respondsToSelector:@selector(window)])
    {
        self.window = [delegate performSelector:@selector(window)];
    }
    else
    {
        self.window = [[UIApplication sharedApplication] keyWindow];
    }
    [self createUI];
    return self;
    
}

-(void)createUI
{
    if (self.label == nil)
    {
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, 64)];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.textColor = [UIColor whiteColor];
        self.label.backgroundColor = Color_With_Rgb(242, 102, 18, 1);
        self.label.font = [UIFont systemFontOfSize:14.f];
        self.label.alpha = 0.f;
    }
    if (self.label.superview == nil)
    {
        [self.window addSubview:self.label];
    }
}

-(void)show
{
    if (!self.label.alpha)
    {
        self.label.alpha = 1.f;
        CGRect rect = self.label.frame;
        rect.origin.y = 0;
        [UIView animateWithDuration:0.5 animations:^{
            self.label.frame = rect;
        } completion:^(BOOL finished) {
            CGRect rect = self.label.frame;
            rect.origin.y = -64;
            [UIView animateWithDuration:0.5 delay:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.label.frame = rect;
            } completion:^(BOOL finished) {
                self.label.alpha = 0.f;
            }];
        }];
    }
}

-(void)showLabelWithString:(NSString *)string
{
    self.label.text = string;
    [self show];
}

@end
